//
//  YZProfessionalInfoVC.m
//  VET
//
//  Created by 刘文靖 on 2018/6/27.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZProfessionalInfoVC.h"
#import "YZOrganizationInfoVC.h"
#import "YZSwineOptionPickerView.h"
#import "YZCityPickerView.h"
#import "YZGradientBtn.h"
#import "YZLauncher.h"
#import "YZUser.h"
#import "YZPersonalVM.h"
@interface YZProfessionalInfoVC ()
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIView *infoIndicatorView;
@property (weak, nonatomic)  IBOutlet UILabel *organizationLabel;
@property (weak, nonatomic) IBOutlet UIView *organizationIndicatorView;
@property (weak, nonatomic)  IBOutlet UILabel *areaLabel;
@property (weak, nonatomic) IBOutlet UIView *areaIndicatorView;
@property (weak, nonatomic) IBOutlet YZGradientBtn *nextButton;
@property (strong, nonatomic) YZPersonalVM *viewModel;
@property (copy, nonatomic) NSString *professionType;
@property (copy, nonatomic) NSString *company;
@property (copy, nonatomic) NSString *province;
@property (copy, nonatomic) NSString *city;
@property (copy, nonatomic) NSArray *dataSource;
@end

@implementation YZProfessionalInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"职业信息";
    //应该先判断用户是否已经登录 如果已经登录说明是从我的tab页面跳转进来  无需 （跳过和下一步）操作
    if (self.needRightItem) {
        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 36, 25)];
        [rightButton setTitle:@"跳过" forState:UIControlStateNormal];
        [rightButton setTitleColor:kColorWithHex(0x007AFF) forState:UIControlStateNormal];
        rightButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
        [rightButton addTarget:self action:@selector(jumpToWorkbanch) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *righBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        self.navigationItem.rightBarButtonItem = righBarButtonItem;
    }else{
        YZUser *loginUser = [YZUser sharedInstance];
        if (loginUser.professionType) {
            self.infoLabel.text = loginUser.professionType;
            self.infoIndicatorView.hidden = YES;
            self.professionType = loginUser.professionType;
        }
        if (loginUser.company) {
            self.organizationLabel.text = loginUser.company;
            self.organizationIndicatorView.hidden = YES;
            self.company = loginUser.company;
        }
        if (loginUser.province) {
            self.areaLabel.text = [NSString stringWithFormat:@"%@ %@", loginUser.province, loginUser.city];
            self.areaIndicatorView.hidden = YES;
            self.province = loginUser.province;
            self.city = loginUser.city;
        }
        [self.nextButton setTitle:@"完成" forState:UIControlStateNormal];
    }
}

- (void)jumpToWorkbanch{
    // 跳到主页
    [YZLauncher jumpToMain];
}
- (IBAction)updateInfo {
    if (self.dataSource.count) {
        [self showProfessionTypePickView:self.infoLabel.text];
    }else{
        @weakify(self)
        [[self.viewModel requestProfessionTypeArr] subscribeNext:^(NSArray *data) {
            @strongify(self)
            self.dataSource = data;//@[@"助理职业兽医师",@"从业兽医人员",@"检疫员",@"饲养员",@"经销商",@"农户"];
            [self showProfessionTypePickView:self.infoLabel.text];
        } completed:^{
        }];
    }
}

- (IBAction)updateOrganization {
    YZOrganizationInfoVC *organizationInfoVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZOrganizationInfoVC"];
        @weakify(self)
        organizationInfoVC.organizationResultBlock = ^(NSString *organizationInfo) {
            @strongify(self)
            self.organizationLabel.text =organizationInfo ;
            self.organizationIndicatorView.hidden = YES;
            self.nextButton.enabled = YES;
            self.company = organizationInfo;
        };
        [self.navigationController pushViewController:organizationInfoVC animated:YES];
}
- (IBAction)updateArea:(id)sender {
    // 【转换】：以@" "自字符串为基准将字符串分离成数组，如：@"浙江省 杭州市 西湖区" ——》@[@"浙江省", @"杭州市", @"西湖区"]
    NSArray *defaultSelArr = [self.areaLabel.text componentsSeparatedByString:@" "];
    // NSArray *dataSource = [weakSelf getAddressDataSource];  //从外部传入地区数据源
    NSArray *dataSource = nil; // dataSource 为空时，就默认使用框架内部提供的数据源（即 BRCity.plist）
    @weakify(self)
    [YZCityPickerView showAddressPickerWithShowType:YZAddressPickerModeCity dataSource:dataSource defaultSelected:defaultSelArr isAutoSelect:NO themeColor:nil resultBlock:^(YZProvinceModel *province, YZCityModel *city, YZAreaModel *area) {
        @strongify(self)
        self.areaLabel.text = [NSString stringWithFormat:@"%@ %@ %@", province.name, city.name, area.name];
        self.province = province.name;
        self.city = city.name;
        self.areaIndicatorView.hidden = YES;
        self.nextButton.enabled = YES;
    } cancelBlock:^{
    }];
}

- (IBAction)nextButtonClicked {
    //1,完善用户信息
    @weakify(self)
    [[self.viewModel updteUserInfoWithProfessionType:self.professionType company:self.company province:self.province city:self.city] subscribeNext:^(NSDictionary *json) {
    } completed:^{
        @strongify(self)
        //2,上传成功后 如果是注册页进来的则 跳转到工作台 否则返回我的tab页面
        if (self.needRightItem) {
            [self jumpToWorkbanch];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (void)showProfessionTypePickView:(NSString *)defaultSelValue{
    @weakify(self)
    [YZSwineOptionPickerView showStringPickerWithTitle:@"选择身份信息"
                                            dataSource:self.dataSource
                                       defaultSelValue: defaultSelValue
                                          isAutoSelect:NO
                                            themeColor:nil
                                           resultBlock:^(id selectValue) {
                                               @strongify(self)
                                               self.infoLabel.text = selectValue;
                                               self.infoIndicatorView.hidden = YES;
                                               self.nextButton.enabled = YES;
                                               self.professionType = selectValue;
                                           }];
}

- (YZPersonalVM *)viewModel{
    if (!_viewModel) {
        _viewModel = [[YZPersonalVM alloc] init];
    }
    return _viewModel;
}

@end
